package goodash

// https://gist.github.com/soroushjp/0ec92102641ddfc3ad5515ca76405f4d
import (
	"bytes"
	"encoding/gob"
)

func init() {
	gob.Register(map[string]any{})
}

// Map performs a deep copy of the given map m.
func DeepCloneMap(m map[string]any) (map[string]any, error) {
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err := enc.Encode(m)
	if err != nil {
		return nil, err
	}
	var cpy map[string]any
	dec := gob.NewDecoder(&buf)
	err = dec.Decode(&cpy)
	if err != nil {
		return nil, err
	}
	return cpy, nil
}
